package com.city.project.dao.impl;

import com.city.project.dao.IStudentDao;
import com.city.project.factory.ConnectionFactory;
import com.city.project.model.Room;
import com.city.project.model.Student;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements IStudentDao {

    private Student gets(ResultSet rs) throws SQLException {
        Student student = new Student();
        student.setAge(rs.getString("age"));
        student.setMajor(rs.getString("major"));
        student.setName(rs.getString("name"));
        student.setRoomname(rs.getString("roomname"));
        student.setSex(rs.getString("sex"));
        return student;
    }

    @Override
    public List<Student> getList(String name) {
        Connection connection = ConnectionFactory.getConnection();
        List<Student> list = new ArrayList<>();
        String sql = "select * from student";
        if (name == null || "".equals(name.trim())) {
            sql = "select * from student where name like '%" + name + "%'";
        }
        PreparedStatement preparedStatement;
        try {
            preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                list.add(gets(resultSet));
            }
            connection.close();
            preparedStatement.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Student getOne(int id) {
        Connection connection = ConnectionFactory.getConnection();
        List<Student> list = new ArrayList<>();
        String sql = "select * from student where id="+id;
        PreparedStatement preparedStatement;
        try {
            preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                list.add(gets(resultSet));
            }
            connection.close();
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list.get(0);
    }

    @Override
    public void add(Student room) {
        Connection connection = ConnectionFactory.getConnection();
        String sql = "insert into student(name,sex,roomname,age,major) values(?,?,?,?,?)";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, room.getName());
            preparedStatement.setString(2, room.getSex());
            preparedStatement.setString(3, room.getRoomname());
            preparedStatement.setString(4, room.getAge());
            preparedStatement.setString(5, room.getMajor());
            preparedStatement.executeUpdate();
            connection.close();
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void update(Student room) {
        Connection connection = ConnectionFactory.getConnection();
        String sql = "update student set name=?,sex=?,roomname=?,age=?,major=? where id=?";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, room.getName());
            preparedStatement.setString(2, room.getSex());
            preparedStatement.setString(3, room.getRoomname());
            preparedStatement.setString(4, room.getAge());
            preparedStatement.setString(5, room.getMajor());
            preparedStatement.setInt(6, room.getId());
            preparedStatement.executeUpdate();
            connection.close();
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void delete(int id) {
        Connection connection = ConnectionFactory.getConnection();
        String sql = "delete from student where id=?";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            preparedStatement.executeUpdate();
            connection.close();
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
